﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataLib.Accounts.Bus;

namespace Ewts.Storage
{
    public partial class LoadStatus : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["UserInfo"] != null)
                {
                    ListItem item = new ListItem();
                    item.Text = "--请选择--";
                    item.Value = "";
                    DataLib.BLL.City bllLib = new DataLib.BLL.City();
 
                    // 处理地
                    dropGraphic.DataSource = bllLib.GetList(" flag1 ='02'");
                    dropGraphic.DataTextField = "cityname";
                    dropGraphic.DataValueField = "cityname";
                    dropGraphic.DataBind();
                    dropGraphic.Items.Insert(0, item);

                    User curUser = new DataLib.Accounts.Bus.User();
                    dropGraphic.Enabled = false;
                    curUser = (User)Session["UserInfo"];
                    // dropGraphic.SelectedItem.Text = curUser.CompanyCity;
                    hidusrName.Value = curUser.UserName;
                    string city = dropGraphic.SelectedItem.Text;
                    if (curUser.UserType == "AD")
                    {
                        dropGraphic.Enabled = true;
                        city = null;
                    }
                }
                else
                {
                    Response.Redirect("/accounts/login.aspx");
                }
                
            }

        }

        //protected void bindData()
        //{
            

        //    rept.DataSource = new DataLib.BLL.CJOItem().GetLoadStatusList(city);
        //    rept.DataBind();
        //}

        /// <summary>
        /// 取得当前状态的中文描述
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        protected string GetStatus(string status,string cid)
        {
            string statusname = "";
            switch (status)
            {
                case "1":
                    statusname = "待派车";
                    break;
                case "2":
                    statusname = "待称重";
                    break;
                case "3":
                    statusname = "待授权";
                    break;
                case "4":
                    {
                        statusname = GetCjoid(cid);
                        break;
                    }
                case "5":
                    {
                        statusname = GetCjoid(cid);
                        statusname = "拆解中";
                        break;
                    }
                case "-4":
                    statusname = "拆解申请审批中";
                    break;
                case "-9":
                    statusname = "作废";
                    break;
                case "-1":
                    statusname = "退运";
                    break;
                case "-2":
                    statusname = "直销";
                    break;
                default:
                    break;
            }
            return statusname;

        }

        private string GetCjoid(string cjoid)
        {
            string flag = "";
            DataLib.Model.Cjo cmodel = new DataLib.BLL.Cjo().GetModel(int.Parse(cjoid));
            if (cmodel != null)
            {
                DataLib.Model.LoadNum lModel = new DataLib.BLL.LoadNum().GetModel(cmodel.loadnumid);
                if (lModel != null)
                {
                    if (lModel.finishWeight == 0)
                    {
                        flag = "待分类";       //待分类
                    }
                    else if (lModel.finishWeight < lModel.totalWeight)
                    {
                        flag = "分类中";       //分类中
                    }
                    else
                    {
                        flag = "分类完成";       //分类完成
                    }
                }
            }
            return flag;
        }

        protected void rept_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "批准")
            {
                DataLib.Model.Cjo model = new DataLib.BLL.Cjo().GetModel(int.Parse(e.CommandArgument.ToString()));
                model.status = 3;
                if (new DataLib.BLL.Cjo().Update(model) == true)
                {
                    DataLib.Common.MessageBox.Show(Page, "已批准，回到待授权");
                }
                //bindData();

            }
        }

        /// <summary>
        /// 获取按钮的状态
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        protected bool GetEnable(string status)
        {
            bool strflag = false;
            if (status == "-4")
            {
                strflag = true;

            }
            return strflag;
        }

        protected void rept_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            LinkButton link1 = (LinkButton)e.Item.FindControl("linkAgree");
            if (link1 != null)
            {
                if (link1.Enabled == true)
                {
                    link1.ForeColor = System.Drawing.Color.Blue; ;
                }
                else
                {
                    link1.ForeColor = System.Drawing.Color.Gray;
                }
            }
        }

        /// <summary>
        /// 获取字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string GetSubString(string str, int len)
        {
            string strreturn = "";
            if (str.Length > len)
            {
                strreturn = str.Substring(0, len) + "...";
            }
            else
            {
                strreturn = str;
            }
            return strreturn;
        }

        //protected void dropGraphic_SelectedIndexChanged(object sender, EventArgs e)
        //{
        
        //    rept.DataSource = new DataLib.BLL.CJOItem().GetLoadStatusList(dropGraphic.SelectedItem.Text);
        //    rept.DataBind();
        //}
    }
}